<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/' }">后台管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <el-form :inline="true" :model="select1" class="demo-form-inline">
      <el-form-item label="书名">
        <el-input v-model="select1.name" placeholder="书名"></el-input>
      </el-form-item>
      <el-form-item label="作者">
        <el-input v-model="select1.author" placeholder="作者"></el-input>
      </el-form-item>
      <el-form-item label="简介">
        <el-input v-model="select1.describe" placeholder="简介"></el-input>
      </el-form-item>
      <el-form-item label="价格">
        <el-input v-model="select1.money"  style="width: 100px" placeholder="价格"></el-input>
      </el-form-item>
      <el-form-item label="库存">
        <el-input v-model="select1.nums" style="width: 100px" placeholder="库存"></el-input>
      </el-form-item>
      <el-form-item label="是否上架">
        <el-input v-model="select1.state" placeholder="是否上架（0下架/1上架)"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button  type="primary" @click="selectLikeGoods(select1)">查询</el-button>
      </el-form-item>
    </el-form>


    <el-divider></el-divider>
    <el-table
        :data="tableData"
        border
        style="width: 100%">
      <el-table-column prop="id" label="编号" width="50" align="center"></el-table-column>
      <el-table-column prop="name" label="书名" width="200" align="center"></el-table-column>
      <el-table-column prop="author" label="作者" width="80" align="center"></el-table-column>
      <el-table-column prop="categoryName" label="类型" width="100" align="center"></el-table-column>
      <el-table-column prop="packName" label="包装类型" width="60" align="center"></el-table-column>
      <el-table-column prop="pressName" label="出版社" width="130" align="center"></el-table-column>
      <el-table-column prop="money" label="价格" width="70" align="center"></el-table-column>
      <el-table-column prop="describe" label="简介" width="200" align="center"></el-table-column>
      <el-table-column prop="sort" label="自定义排序" width="70" align="center"></el-table-column>
      <el-table-column prop="nums" label="销量" width="70" align="center"></el-table-column>
      <el-table-column prop="num" label="库存" width="60" align="center"></el-table-column>
      <el-table-column prop="state"label="是否上架" width="80" align="center">
        <template slot-scope="scope">
          <el-switch
              v-model="scope.row.state"
              @change="changEnable(scope.row)"
              :active-value="1"
              :inactive-value="0"
              active-color="#13ce66"
              inactive-color="#ff4949">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="houseTime" label="出版时间"  width="130"  align="center"></el-table-column>
      <el-table-column prop="createTime" label="数据创建时间" width="130"  align="center"></el-table-column>
      <el-table-column label="操作" width="120" align="center">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" circle size="mini" @click="handleEdit(scope.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" circle size="mini" @click="openDelectConfirm(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>

  </div>
</template>
<script>
import axios from "axios";

export default {
  data() {
    return {
      tableData: [],

      select1:{},
      ruleForm: {
        name: 'wqeqwe',
        region: '',
        date1: '',
        date2: '',
        delivery: false,
        type: [],
        resource: '',
        desc: ''
      },
    }
  },
  methods:{
    selectLikeGoods(select1){
      let s=this.qs.stringify(this.select1)
      console.log("模糊查询select1=",s)
      let url='http://localhost:8080/adminGoodsAddNew/selectLikeGoods';
      this.axios.post(url,s).then((response)=>{
        let responseBody=response.data;
        if(responseBody.state==20000){
          this.tableData=responseBody.data;
          this.$message({
            type: 'success',
            message: "查询成功"
          });
        }else {
          this.$message({
            type: 'info',
            message: responseBody.message
          });
        }
      })
    },
    changEnable(goods){
      let enableText = ['下架', '上架'];
      let url = 'http://localhost:8080/adminGoodsAddNew/state/' + goods.id;
      if(goods.state==1){
        console.log('将上架该图书');
        url += '/enable';
      } else {
        console.log('将下架该图书');
        url += '/disable';
      }
      console.log('url = ' + url);
      this.axios.post(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          let message = '将商品【' + goods.name + '】的状态改为【' + enableText[goods.state] + '】成功！';
          this.$message({
            message: message,
            type: 'success'
          });
        } else {
          this.$message.error(responseBody.message);
        }
        if (responseBody.state == 40400) {
          this.loadGoodsList();
        }
      });
    },
    handleEdit(goods) {
      let url='http://localhost:8080/adminGoodsAddNew/'+goods.id+'/selectGoodsById';
      this.axios.get(url).then((response)=>{
        let responseBody=response.data;
        if(responseBody.state==20000){
          this.ruleForm=response.data;
          let ruleFormString = JSON.stringify(this.ruleForm);
          localStorage.setItem('ruleForm', ruleFormString);
          console.log("111111",this.ruleForm)
          this.$router.push('updategoods');
        }else {
          this.$message({
            type: 'info',
            message: responseBody.message
          });
          this.loadGoodsList();
        }
      })

    },
    handleDelect(brand){
      console.log("准备删除"+brand.id)
      let url='http://localhost:8080/adminGoodsAddNew/'+brand.id+'/delete';
      this.axios.post(url).then((response)=>{
        let responseBody=response.data;
        if(responseBody.state==20000){
          this.loadGoodsList();
          this.$message({
            type: 'success',
            message: '删除成功'
          });
        }else {
          this.$message({
            type: 'info',
            message: responseBody.message
          });
          this.loadGoodsList();
        }
      })
    },
    openDelectConfirm(brand){
      let message='此操作将永久删除该图书《'+brand.name+'》和它的！！商品图片！！, 是否继续???';
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelect(brand);
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    loadGoodsList(){
      console.log('loadAlbumList方法')
      let url='http://localhost:8080/adminGoodsAddNew/goodslist';
      console.log('url='+url);
      this.axios.get(url).then((response)=>{
        let responseBody=response.data;
        this.tableData=responseBody.data;
      })
    }
  },
  mounted() {//挂载
    console.log('mounted')
    this.loadGoodsList();
  }

}
</script>